package com.lemon.reggie.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lemon.reggie.common.R;
import com.lemon.reggie.pojo.dto.AddDishDTO;
import com.lemon.reggie.pojo.dto.EditDishDTO;
import com.lemon.reggie.pojo.vo.DishVO;
import com.lemon.reggie.pojo.vo.GetDishVO;
import com.lemon.reggie.service.DishService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 菜品管理 前端控制器
 * </p>
 *
 * @author Lemon
 * @since 2022-05-23
 */
@RestController
@RequestMapping("/dish")
@Validated
public class DishController {

    @Resource
    private DishService dishService;

    @PostMapping
    public R<?> addDish(@RequestBody @Valid AddDishDTO addDishDTO) {
        dishService.addDish(addDishDTO);
        return R.success("添加成功！");
    }

    @GetMapping("/page")
    public R<?> pageDish(@RequestParam("page") Long currentPage,
                         @RequestParam("pageSize") Long pageSize,
                         @RequestParam(value = "name", required = false) String name) {
        IPage<DishVO> pages = dishService.pageDish(currentPage, pageSize, name);
        return R.success(pages);
    }

    @DeleteMapping
    public R<?> deleteDish(@RequestParam("ids") List<Long> ids) {
        dishService.deleteDish(ids);
        return R.success("删除成功！");
    }

    @PostMapping("/status/{status}")
    public R<?> statusDish(@PathVariable("status") Integer status,
                           @RequestParam("ids") List<Long> ids) {
        dishService.statusDish(status, ids);
        return R.success(status == 1 ? "启售成功！" : "停售成功！");
    }

    @GetMapping("/{id}")
    public R<?> getDish(@PathVariable Long id) {
        GetDishVO getDishVO = dishService.getDish(id);
        return R.success(getDishVO);
    }

    @PutMapping
    public R<?> editDish(@RequestBody @Valid EditDishDTO editDishDTO) {
        dishService.editDish(editDishDTO);
        return R.success("修改成功！");
    }

    @GetMapping("/list")
    public R<?> listDish(Long categoryId, String name) {
        List<DishVO> dishVOList = dishService.listDish(categoryId, name);
        return R.success(dishVOList);
    }
}

